// The Swift Programming Language
// https://docs.swift.org/swift-book

import Foundation

public struct XGLogger {
    
    private let formatter: DateFormatter
    private let isFile: Bool
    private let isQueue: Bool
    private let isLine: Bool
    
    public init() {
        formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSSSSSZ"
        isFile = true
        isQueue = true
        isLine = true
    }
    
    public func verbose(_ item: Any, file: String = #file, line: Int = #line, date: Date = Date()) {
        dd(item, time: formatter.string(from: Date()), file: file, line: line, type: "Verbose")
    }
    
    public func debug(_ item: Any, file: String = #file, line: Int = #line, date: Date = Date()) {
        dd(item, time: formatter.string(from: Date()), file: file, line: line, type: "Debug")
    }
    
    public func info(_ item: Any, file: String = #file, line: Int = #line, date: Date = Date()) {
        dd(item, time: formatter.string(from: Date()), file: file, line: line, type: "Info")
    }
    
    public func error(_ item: Any, file: String = #file, line: Int = #line, date: Date = Date()) {
        dd(item, time: formatter.string(from: Date()), file: file, line: line, type: "Error")
    }
    
    private func dd(_ item: Any,
                    time: String,
                    file: String,
                    line: Int,
                    type: String) {
        #if DEBUG
        var message = "\(time) "
        let fileName = URL(fileURLWithPath: file, isDirectory: true).lastPathComponent
        if isFile {
            message += "[\(fileName):\(line)] "
        }
        
        if isQueue {
            message += Thread.isMainThread ? "MAIN" : "GLOBAL"
        }
        message += "[\(type)]:"
        message += "> \(item)"
        print(message)
        #endif
    }
}

/*
 update v1.0.1
 */

